package com.ggs.starbug.consumer;

import com.ggs.starbug.entity.TimerTask;
import com.ggs.starbug.thread.TimerTaskSchedule;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

import static com.ggs.starbug.config.rabbitmq.RabbitMQConstants.*;

/**
 * @Author lianghaohui
 * @Date 2021/12/27 12:28
 * @Description
 */
@Slf4j
@Component
public class MessageConsumer {

    @Autowired
    private volatile TimerTaskSchedule timerTaskSchedule;

    @Autowired
    private Gson gson;

    private int i = 0;

    @RabbitHandler
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = TIMER_QUEUE),
//            exchange = @Exchange(value = TIMER_TOPIC_EXCHANGE, type = "topic"),
//            key = "timer.#"
//    )
//    )
    @RabbitListener(queues = TIMER_ADD_QUEUE, ackMode = "MANUAL")
    public void timerAddQueueConsumer(@Payload TimerTask timerTask, @Headers Map<String, Object> headers, Channel channel, Message message) throws IOException {
//        ScheduledTask scheduledTask = timerTaskSchedule.getScheduledTaskRegistrar().scheduleCronTask(new CronTask(() -> {
        System.out.println("从timer_add_queue中获取消息：" + timerTask);
//            log.info("{}----{}", Thread.currentThread().getName(), gson.toJson(timerTask));
//        }, new CronTrigger(timerTask.getCron(), ZoneId.systemDefault())));
//        timerTaskSchedule.putScheduledFutures(timerTask.getId(), scheduledTask);
        timerTaskSchedule.addTimerTask(timerTask);
        i++;
//        if (i % 5 == 0) {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        }else{
//            channel.basicNack();
//        }
//        if ("starbug".equals(timerTask.getDescription())) {
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        } else {
////            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        }
    }

    @RabbitHandler
    @RabbitListener(queues = TIMER_DELETE_QUEUE)
    public void timerDeleteQueueConsumer(@Payload TimerTask timerTask, @Headers Map<String, Object> headers, Channel channel) {
        System.out.println("从timer_delete_queue中获取消息：" + timerTask);
        timerTaskSchedule.deleteTimerTask(timerTask);
    }

    @RabbitHandler
    @RabbitListener(queues = TIMER_UPDATE_QUEUE)
    public void timerUpdateQueueConsumer(@Payload TimerTask timerTask, @Headers Map<String, Object> headers, Channel channel) {
        System.out.println("从timer_update_queue中获取消息：" + timerTask);
        timerTaskSchedule.deleteTimerTask(timerTask);
        timerTaskSchedule.addTimerTask(timerTask);
    }

}
